# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=angleproject
pkgbase=mingw-w64-${_realname}-git
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
pkgver=2.1.r5864
pkgrel=1
pkgdesc='ANGLE project built from git source (mingw-w64)'
arch=('any')
url='https://chromium.googlesource.com/angle/angle'
license=('LGPLv2+')
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" 'gyp-git' 'git')
provides=("${MINGW_PACKAGE_PREFIX}-angleproject")
conflicts=("${MINGW_PACKAGE_PREFIX}-angleproject")
options=('!strip' 'staticlibs')
source=("angleproject"::git+https://chromium.googlesource.com/angle/angle
        0000-build-fix.patch
        angleproject-include-import-library-and-use-def-file.patch
        libEGL_mingw32.def
        libGLESv2_mingw32.def
        entry_points_shader.cpp)
sha256sums=('SKIP'
            '80f1dc27536181e6cce98089de36f2829a525b1e134e733bbe4083607ace45e5'
            'b96ba955a352ff39aae44bf8d6f3230f200607aa0fecd1e2a8b69ca3bb2bbda8'
            'fb04f30b904760d32c4c0b733d0a0b44359855db1fde9e7f5ca7d0b8b1be3e56'
            '3186d913a5fb483d2ae568068453e494d52df8f3f23f09d16afbbf916a63e4a4'
            'ad347c9732f8897497aa51b8969a0e01cd8cd4ebb9a0e873a2ff47c210f1d46c')
pkgver() {
  cd "$srcdir/angleproject"
  local _major=$(head -n 14 src/common/version.h | grep 'ANGLE_MAJOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  local _minor=$(head -n 14 src/common/version.h | grep 'ANGLE_MINOR_VERSION' | sed -e 's/.* //' | tr '\n' '.' | sed 's/.$/\n/')
  printf "%s.%s.r%s" "$_major" "$_minor" "$(git rev-list --count HEAD)"
}

prepare() {
  cd "${srcdir}"/${_realname}

  # provide 32-bit versions of *.def files
  cp ../libEGL_mingw32.def src/libEGL/
  cp ../libGLESv2_mingw32.def src/libGLESv2/
  
  # provide a file to export symbols declared in ShaderLang.h as part of libGLESv2.dll
  # (required to build Qt WebKit which uses the shader interface)
  cp ../entry_points_shader.cpp src/libGLESv2/

  ### Fedora team patches ###
  patch -p1 -i ${srcdir}/0000-build-fix.patch

  # Make sure an import library is created and the correct .def file is used during the build
  # and entry_points_shader.cpp is compiled
  patch -p1 -i ${srcdir}/angleproject-include-import-library-and-use-def-file.patch

  #echo "" > src/copy_compiler_dll.bat
  #chmod +x src/copy_compiler_dll.bat
}

build() {
  cd "${srcdir}"/angleproject

  export PYTHON=/usr/bin/python2

  #export CFLAGS="-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4"
  #export CXXFLAGS="-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4"

  sed -i -e 's_python _python2 _g' -e 's_"python"_"python2"_g' -e "s_'python'_'python2'_g" -e 's_/usr/bin/python_/usr/bin/python2_g' $(find -not \( -path "./.git*" -prune \) -type f)

  if [[ "${MINGW_CHOST}" == "i686-w64-mingw32" ]]; then
    _target="win32"
  else
    _target="win64"
  fi

  gyp -D use_ozone=0 -D OS=win -D MSVS_VERSION="" -D TARGET=${_target} --format make --depth . -I build/common.gypi src/angle.gyp

  # Make sure the correct libraries are linked in
  #sed -i s@'^LIBS :='@'LIBS := -ld3d9 -ldxguid'@ ../src/libGLESv2.target.mk
  #sed -i s@'^LIBS :='@'LIBS := -ld3d9 -ldxguid -L. -lGLESv2'@ ../src/libEGL.target.mk

  # LINK=g++ is to prevent using flock to serialize linking.
  LINK=g++ make -j1 V=1 CXXFLAGS="-std=c++11 -msse2 -DUNICODE -D_UNICODE"
}

package() {
  cd "${srcdir}"/angleproject

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/{bin,lib,include}

  install out/Debug/lib.target/libGLESv2.dll "${pkgdir}${MINGW_PREFIX}"/bin/libGLESv2.dll
  install out/Debug/lib.target/libEGL.dll "${pkgdir}${MINGW_PREFIX}"/bin/libEGL.dll
  ${MINGW_PREFIX}/bin/strip --strip-unneeded "${pkgdir}${MINGW_PREFIX}"/bin/*.dll

  #install libGLESv2.a "${pkgdir}${MINGW_PREFIX}"/lib/
  #install libEGL.a "${pkgdir}${MINGW_PREFIX}"/lib/

  install libEGL.dll.a "${pkgdir}${MINGW_PREFIX}"/lib/
  install libGLESv2.dll.a "${pkgdir}${MINGW_PREFIX}"/lib/
  ${MINGW_PREFIX}/bin/strip --strip-unneeded "${pkgdir}${MINGW_PREFIX}"/lib/*.dll.a

  cp -Rv include/* "${pkgdir}${MINGW_PREFIX}"/include/
}
